package com.example.justbuy.action.buyer;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.justbuy.pojo.PaySaPi;
import com.example.justbuy.service.OrderMapperService;
import com.example.justbuy.util.paysapi.PayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class PayController {

	@Autowired
	private OrderMapperService orderMapperService;
	@RequestMapping("pay_true.htm")
	public String pay_ture(String orderId, float Allprice, Model md,String address){
		HashMap mp1=new HashMap();
		mp1.put("address",address);
		mp1.put("orderId",orderId);
		orderMapperService.updateaddressId(mp1);
		md.addAttribute("orderId",orderId);
		md.addAttribute("Allprice",Allprice);
		return "paysapi.html";
	}

	@RequestMapping("/pay")
	@ResponseBody
	public Map<String, Object> pay(HttpServletRequest request, float price, int istype,String orderId) throws Exception{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> remoteMap = new HashMap<String, Object>();
		remoteMap.put("price", price);
		remoteMap.put("istype", istype);
		String payId= PayUtil.getOrderIdByUUId();
		HashMap mp1=new HashMap();
		mp1.put("payId",payId);
		mp1.put("orderId",orderId);
		int flag=orderMapperService.updatePayIdById(mp1);
		System.out.println(flag);
		remoteMap.put("orderid", payId);
		remoteMap.put("orderuid", orderId);
		remoteMap.put("goodsname", "您自己的商品名称");
		resultMap.put("data", PayUtil.payOrder(remoteMap));
		return resultMap;
	}

	@RequestMapping("/notifyPay.htm")
	public@ResponseBody String notifyPay(HttpServletRequest request, HttpServletResponse response, PaySaPi paySaPi)  throws Exception{
		// 保证密钥一致性
		//System.out.print("匹配成功");
		/*if (PayUtil.checkPayKey(paySaPi)) {
			// TODO 做自己想做的
			System.out.print(paySaPi.getOrderuid());
			System.out.print("匹配成功");
		} else {
			// TODO 该怎么做就怎么做
			System.out.print("匹配不成功");
		}*/
		return "200";
	}

	@RequestMapping("/returnPay.htm")
	public void returnPay(HttpServletRequest request, HttpServletResponse response, String orderid) throws Exception{

		System.out.print(orderid);
		HashMap mp1=new HashMap();
		mp1.put("payId",orderid);
		mp1.put("orderState","已付款");
		int flag=orderMapperService.updateStateBypayId(mp1);
			response.sendRedirect("/");

	}
}
